﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.IO;

namespace VINETLIFE.AdminCP
{
    public partial class OrderStatus : System.Web.UI.Page
    {
        cmsTransactionStatusDO objTran = new cmsTransactionStatusDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            //bindToDropDown(cboCategoryCate);
            //Functions.ddlDatabinder(cboTrangThai, cmsSytemStatusDO.CREATEDBYID_FIELD, cmsSytemStatusDO.TITLE_FIELD, new cmsCategoryBL().SelectAdmin(false));
            if (Request.QueryString["TransactionStatusId"] != null)
            {
                objTran.TransactionStatusId = int.Parse(Request.QueryString["TransactionStatusId"].ToString());
                NgayTao.Visible = true;
                if (!IsPostBack)
                {
                    initForm();
                }
            }
        }

        private void initForm()
        {
            hplImage.Visible = true;
            objTran = new cmsTransactionStatusBL().Select(objTran);
            txtTrangThaiDH.Text = objTran.TransactionStatus;
            txtMaTrangThai.Text = objTran.TransactionStatusCode;
            txtNote.Text = objTran.Note;
            hplImage.NavigateUrl = "~/Media/" + objTran.Icon;
            txtNgayTao.Text = objTran.CreatedDate.ToString();
            if (objTran.LastSaveDate != objTran.CreatedDate)
            {
                NgaySua.Visible = true;
                txtNgaySua.Text = objTran.LastSaveDate.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objTran.TransactionStatusId <= 0)
            {
                objTran.CreatedDate = DateTime.Now;
                objTran.LastSaveDate = DateTime.Now;
                new cmsTransactionStatusBL().Insert(objTran);
            }
            else
            {
                objTran.LastModifiedById = int.Parse(Session["UserID"].ToString());
                objTran.LastSaveDate = DateTime.Now;
                new cmsTransactionStatusBL().Update(objTran);
            }
            Functions.Alert("Cập nhật thành công!", "ListOrderStatus.aspx");
        }
        private void initObject()
        {
            objTran = new cmsTransactionStatusBL().Select(objTran);
            objTran.TransactionStatus = txtTrangThaiDH.Text;
            objTran.TransactionStatusCode = txtMaTrangThai.Text;
            objTran.Note = txtNote.Text;
            objTran.CreatedById = int.Parse(Session["UserID"].ToString());
            if (!string.IsNullOrEmpty(fuImage.FileName))
                objTran.Icon = UploadFile(fuImage);
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTrangThaiDH.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                FileName = FileName.Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(" ", "-").Replace("/", "-").Replace("?", "-").Replace("\\", "-").Replace("'", "-").Replace(":", "-").Replace("\"", "-");
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/ListOrderStatus.aspx");
        }
    }
}